using MicroMsg.Manager;
using MicroMsg.Storage;
using MicroMsg.UI.UserContrl;
using MicroMsg.UI.Util;
using Microsoft.Phone.Controls;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.IO.IsolatedStorage;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Resources;
namespace MicroMsg.UI.Page
{
	public class SelectCountryPage : BasePage
	{
		public class Group<T> : List<T>
		{
			public string Key
			{
				get;
				set;
			}
			public bool HasItems
			{
				get
				{
					return 0 != base.get_Count();
				}
				set
				{
				}
			}
			public Group(string name, IEnumerable<T> items)
			{
				this.Key = name;
				using (IEnumerator<T> enumerator = items.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						T current = enumerator.get_Current();
						base.Add(current);
					}
				}
			}
			public override bool Equals(object value)
			{
				SelectCountryPage.Group<T> group = value as SelectCountryPage.Group<T>;
				return group != null && this.Key.Equals(group.Key);
			}
			public override int GetHashCode()
			{
				return base.GetHashCode();
			}
		}
		public static string sStrSelectedCountry = null;
		public static bool shoudCloseOnBack = false;
		private DelegateSelectArea mCallBack;
		private bool isFoundInList;
		private static string loadedLanguage;
		private static List<SelectCountryPage.Group<string>> countryData;
		private static Dictionary<string, List<SelectStatePage.Group<string>>> dataMap;
		private object mGroupHeaderLocker = new object();
		public bool sBIsSelectorShown;
		private ScrollViewer mScrollViewer;
		internal Grid LayoutRoot;
		internal StackPanel ContentPanel;
		internal panelTitleControl panel1Line;
		internal LongListSelector mListGroupAreas;
		private bool _contentLoaded;
		public SelectCountryPage()
		{
			SelectCountryPage.shoudCloseOnBack = false;
			this.InitializeComponent();
			SelectCountryPage.sStrSelectedCountry = null;
			base.add_Loaded(new RoutedEventHandler(this.onPageLoaded));
		}
		private void onPageLoaded(object sender, RoutedEventArgs e)
		{
			if (SelectCountryPage.sStrSelectedCountry != null && SelectCountryPage.sStrSelectedCountry.get_Length() > 0 && this.isFoundInList)
			{
				this.mListGroupAreas.ScrollTo(SelectCountryPage.sStrSelectedCountry);
			}
			Canvas.SetLeft(this.mListGroupAreas, 0.0);
		}
		protected override void OnPageIn(object[] object_0)
		{
			if (object_0 != null && 1 == object_0.Length && object_0[0] != null && object_0[0] is DelegateSelectArea)
			{
				this.mCallBack = (object_0[0] as DelegateSelectArea);
			}
			SelectCountryPage.sStrSelectedCountry = AccountMgr.getCurAccount().strProvince;
			this.loadData();
			using (Dictionary<string, List<SelectStatePage.Group<string>>>.KeyCollection.Enumerator enumerator = SelectCountryPage.dataMap.get_Keys().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					string current = enumerator.get_Current();
					if (current == SelectCountryPage.sStrSelectedCountry)
					{
						this.isFoundInList = true;
						break;
					}
				}
			}
			if (!this.isFoundInList)
			{
				if (SelectAreaPage.isChineseProvince(SelectCountryPage.sStrSelectedCountry))
				{
					SelectCountryPage.sStrSelectedCountry = this.getChinaName();
					this.isFoundInList = true;
				}
				else
				{
					if (!R.isEnglish())
					{
						SelectCountryPage.sStrSelectedCountry = this.getChinaName();
						this.isFoundInList = true;
					}
				}
			}
			if (this.mListGroupAreas.ItemsSource == null)
			{
				this.mListGroupAreas.ItemsSource = SelectCountryPage.countryData;
			}
		}
		protected override void OnPageOut()
		{
		}
		protected override void OnPageBack()
		{
			if (SelectCountryPage.shoudCloseOnBack)
			{
				this.LayoutRoot.set_Visibility(1);
				if (this.mCallBack != null)
				{
					this.mCallBack(AccountMgr.getCurAccount().strProvince, AccountMgr.getCurAccount().strCity);
				}
				base.GoBack();
			}
		}
		protected override void OnBackKeyPress(CancelEventArgs cancelEventArgs_0)
		{
			if (this.mCallBack != null)
			{
				this.mCallBack(AccountMgr.getCurAccount().strProvince, AccountMgr.getCurAccount().strCity);
			}
		}
		private void PhoneCountry_ItemClick(object sender, object object_0)
		{
			string text = object_0 as string;
			if (text != null)
			{
				if (this.isChina(text))
				{
					base.GoToPage("/Source/UI/Setting/SelectAreaPage.xaml", new object[]
					{
						new DelegateSelectArea(SelectCountryPage.saveAreaData)
					}, false);
					return;
				}
				List<SelectStatePage.Group<string>> list = null;
				if (SelectCountryPage.dataMap.ContainsKey(text))
				{
					list = SelectCountryPage.dataMap.get_Item(text);
				}
				if (list == null || list.get_Count() == 0)
				{
					SelectCountryPage.saveAreaData(text, "");
					if (this.mCallBack != null)
					{
						this.mCallBack(AccountMgr.getCurAccount().strProvince, AccountMgr.getCurAccount().strCity);
					}
					base.GoBack();
					return;
				}
				if (AccountMgr.getCurAccount().strProvince != text)
				{
					SelectCountryPage.saveAreaData(text, "");
				}
				base.GoToPage("/Source/UI/Setting/SelectStatePage.xaml", new object[]
				{
					list
				}, false);
			}
		}
		private static void saveAreaData(string strProvince, string strCity)
		{
			SelfPersonCard selfPersonCard = AccountMgr.getSelfPersonCard();
			if (selfPersonCard != null)
			{
				if (strProvince != null && strProvince.get_Length() > 0)
				{
					selfPersonCard.strProvince = SelectCountryPage.trimToEmpty(strProvince);
				}
				if (strCity != null)
				{
					selfPersonCard.strCity = SelectCountryPage.trimToEmpty(strCity);
				}
				AccountMgr.setSelfPersonCard(selfPersonCard);
				Contact contact = StorageMgr.contact.method_0(AccountMgr.curUserName);
				if (contact != null)
				{
					contact.strCity = selfPersonCard.strCity;
					contact.strProvince = selfPersonCard.strProvince;
					StorageMgr.contact.modify(contact);
				}
			}
		}
		private static string trimToEmpty(string string_0)
		{
			if (string_0 != null)
			{
				return string_0.Trim().Replace("\n", "");
			}
			return "";
		}
		public void loadData()
		{
			string currentLanguage = R.GetCurrentLanguage();
			if (currentLanguage == SelectCountryPage.loadedLanguage)
			{
				return;
			}
			string text = this.readDataString();
			Dictionary<string, List<SelectStatePage.Group<string>>> dictionary = new Dictionary<string, List<SelectStatePage.Group<string>>>();
			List<SelectCountryPage.Group<string>> list = new List<SelectCountryPage.Group<string>>();
			string text2 = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ#";
			for (int i = 0; i < text2.get_Length(); i++)
			{
				list.Add(new SelectCountryPage.Group<string>(text2.get_Chars(i).ToString(), new List<string>()));
			}
			List<string> list2 = Enumerable.ToList<string>(text.Split(new string[]
			{
				"||"
			}, 1));
			if (list2 != null)
			{
				using (List<string>.Enumerator enumerator = list2.GetEnumerator())
				{
					while (enumerator.MoveNext())
					{
						string current = enumerator.get_Current();
						List<string> list3 = Enumerable.ToList<string>(current.Split(new string[]
						{
							"@"
						}, 1));
						List<string> list4 = Enumerable.ToList<string>(list3.get_Item(0).Split(new string[]
						{
							":"
						}, 1));
						if (list4 != null)
						{
							string text3 = list4.get_Item(0);
							string text4 = list4.get_Item(1);
							if (text3 != null && text4 != null)
							{
								char c = text4.ToLower().get_Chars(0);
								int num = (int)(c - 'a' + '\u0001');
								if (num >= 1 && num < list.get_Count() - 1)
								{
									list.get_Item(num).Add(text3);
								}
								dictionary.Add(text3, null);
								if (list3.get_Count() > 1 && list3.get_Item(1) != null && list3.get_Item(1).get_Length() > 0)
								{
									List<string> list5 = Enumerable.ToList<string>(list3.get_Item(1).Split(new string[]
									{
										";"
									}, 1));
									if (list5 != null && list5.get_Count() != 0)
									{
										List<SelectStatePage.Group<string>> list6 = new List<SelectStatePage.Group<string>>();
										string text5 = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ#";
										for (int j = 0; j < text5.get_Length(); j++)
										{
											list6.Add(new SelectStatePage.Group<string>(text5.get_Chars(j).ToString(), new List<string>()));
										}
										using (List<string>.Enumerator enumerator2 = list5.GetEnumerator())
										{
											while (enumerator2.MoveNext())
											{
												string current2 = enumerator2.get_Current();
												List<string> list7 = Enumerable.ToList<string>(current2.Split(new string[]
												{
													":"
												}, 1));
												if (list7 != null)
												{
													string text6 = list7.get_Item(0);
													string text7 = list7.get_Item(1);
													if (text6 != null && text7 != null)
													{
														char c2 = text7.ToLower().get_Chars(0);
														int num2 = (int)(c2 - 'a' + '\u0001');
														if (num2 >= 1 && num2 < list6.get_Count() - 1)
														{
															list6.get_Item(num2).Add(text6);
														}
													}
												}
											}
										}
										if (list6.get_Count() > 0)
										{
											this.sortData(list6);
											dictionary.Remove(text3);
											dictionary.Add(text3, list6);
										}
									}
								}
							}
						}
					}
				}
				this.sortData(list);
				SelectCountryPage.countryData = list;
				SelectCountryPage.dataMap = dictionary;
				SelectCountryPage.loadedLanguage = currentLanguage;
			}
		}
		private void sortData(List<SelectCountryPage.Group<string>> listGroups)
		{
			using (List<SelectCountryPage.Group<string>>.Enumerator enumerator = listGroups.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SelectCountryPage.Group<string> current = enumerator.get_Current();
					current.Sort(new Comparison<string>(string.Compare));
				}
			}
		}
		private void sortData(List<SelectStatePage.Group<string>> listGroups)
		{
			using (List<SelectStatePage.Group<string>>.Enumerator enumerator = listGroups.GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					SelectStatePage.Group<string> current = enumerator.get_Current();
					current.Sort(new Comparison<string>(string.Compare));
				}
			}
		}
		private string readDataString()
		{
			string result = null;
			string text;
			if (R.isChineseCN())
			{
				text = "Resource/Txt/countryData_CN.txt";
			}
			else
			{
				if (R.isChineseTW())
				{
					text = "Resource/Txt/countryData_TW.txt";
				}
				else
				{
					text = "Resource/Txt/countryData_EN.txt";
				}
			}
			IsolatedStorageFile.GetUserStoreForApplication();
			StreamResourceInfo resourceStream = Application.GetResourceStream(new Uri(text, 2));
			using (BinaryReader binaryReader = new BinaryReader(resourceStream.get_Stream()))
			{
				byte[] array = binaryReader.ReadBytes((int)resourceStream.get_Stream().get_Length());
				result = Encoding.GetEncoding("UTF-8").GetString(array, 0, array.Length);
				binaryReader.Close();
			}
			return result;
		}
		private void GroupHeaderClick(object sender, object object_0)
		{
			this.ShownSelector();
		}
		public void ShownSelector()
		{
			lock (this.mGroupHeaderLocker)
			{
				if (this.sBIsSelectorShown)
				{
					return;
				}
				this.sBIsSelectorShown = true;
			}
			this.mListGroupAreas.DisplayGroupView();
		}
		private void mListGroupAreas_GroupViewOpened(object sender, GroupViewOpenedEventArgs e)
		{
			QuadraticEase quadraticEase = new QuadraticEase();
			quadraticEase.set_EasingMode(0);
			IEasingFunction easingFunction = quadraticEase;
			Storyboard storyboard = new Storyboard();
			ItemsControl itemsControl = e.ItemsControl;
			using (IEnumerator<object> enumerator = itemsControl.get_Items().GetEnumerator())
			{
				while (enumerator.MoveNext())
				{
					object current = enumerator.get_Current();
					UIElement uIElement = itemsControl.get_ItemContainerGenerator().ContainerFromItem(current) as UIElement;
					if (uIElement != null)
					{
						Border border = VisualTreeHelper.GetChild(uIElement, 0) as Border;
						if (border != null)
						{
							DoubleAnimationUsingKeyFrames doubleAnimationUsingKeyFrames = new DoubleAnimationUsingKeyFrames();
							EasingDoubleKeyFrame easingDoubleKeyFrame = new EasingDoubleKeyFrame();
							easingDoubleKeyFrame.set_KeyTime(TimeSpan.FromMilliseconds(0.0));
							easingDoubleKeyFrame.set_Value(-90.0);
							easingDoubleKeyFrame.set_EasingFunction(easingFunction);
							EasingDoubleKeyFrame easingDoubleKeyFrame2 = new EasingDoubleKeyFrame();
							easingDoubleKeyFrame2.set_KeyTime(TimeSpan.FromMilliseconds(500.0));
							easingDoubleKeyFrame2.set_Value(0.0);
							easingDoubleKeyFrame2.set_EasingFunction(easingFunction);
							doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame);
							doubleAnimationUsingKeyFrames.get_KeyFrames().Add(easingDoubleKeyFrame2);
							Storyboard.SetTargetProperty(doubleAnimationUsingKeyFrames, new PropertyPath(PlaneProjection.RotationXProperty));
							Storyboard.SetTarget(doubleAnimationUsingKeyFrames, border.get_Projection());
							storyboard.get_Children().Add(doubleAnimationUsingKeyFrames);
						}
					}
				}
			}
			storyboard.Begin();
		}
		private void mListGroupAreas_GroupViewClosing(object sender, GroupViewClosingEventArgs e)
		{
			e.Cancel = true;
			if (this.mListGroupAreas == null)
			{
				return;
			}
			this.mListGroupAreas.CloseGroupView();
			if (e.SelectedGroup != null)
			{
				if (this.mScrollViewer == null)
				{
					this.mListGroupAreas.ScrollToGroup(e.SelectedGroup);
				}
				else
				{
					SelectCountryPage.Group<string> group = e.SelectedGroup as SelectCountryPage.Group<string>;
					double num = 0.0;
					using (List<SelectCountryPage.Group<string>>.Enumerator enumerator = SelectCountryPage.countryData.GetEnumerator())
					{
						while (enumerator.MoveNext())
						{
							SelectCountryPage.Group<string> current = enumerator.get_Current();
							if (current.Key == group.Key)
							{
								break;
							}
							if (current.get_Count() != 0)
							{
								num += (double)(current.get_Count() + 1);
							}
						}
					}
					this.mScrollViewer.ScrollToVerticalOffset(num);
				}
			}
			this.sBIsSelectorShown = false;
		}
		private void regEventHandForList()
		{
			if (this.mScrollViewer != null)
			{
				return;
			}
			ScrollViewer scrollViewer = UIElementTraveler.FindElementRecursive(this.mListGroupAreas, typeof(ScrollViewer)) as ScrollViewer;
			this.mScrollViewer = scrollViewer;
			this.mListGroupAreas.ScrollingCompleted += new EventHandler(this.mListGroupAreas_ScrollingCompleted);
		}
		private void mListGroupAreas_ScrollingCompleted(object sender, EventArgs e)
		{
			if (this.mScrollViewer != null)
			{
				this.mScrollViewer.ScrollToVerticalOffset(this.mScrollViewer.get_VerticalOffset());
			}
		}
		private string getChinaName()
		{
			if (R.isChineseCN())
			{
				return "中国";
			}
			if (R.isChineseTW())
			{
				return "中國";
			}
			return "China";
		}
		private bool isChina(string country)
		{
			return country != null && (country == "中国" || country == "中國" || country == "China");
		}
		[DebuggerNonUserCode]
		public void InitializeComponent()
		{
			if (this._contentLoaded)
			{
				return;
			}
			this._contentLoaded = true;
			Application.LoadComponent(this, new Uri("/MicroMsg;component/Source/UI/Setting/SelectCountryPage.xaml", 2));
			this.LayoutRoot = (Grid)base.FindName("LayoutRoot");
			this.ContentPanel = (StackPanel)base.FindName("ContentPanel");
			this.panel1Line = (panelTitleControl)base.FindName("panel1Line");
			this.mListGroupAreas = (LongListSelector)base.FindName("mListGroupAreas");
		}
	}
}
